package com.aaa.redis;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 *ApplicationContextAware是一个接口，spring容器初始化之后，将 ApplicationContext注入到本类中的
 * 静态变量applicationContext中
 * new ClassPathXmlApplicationContext()会重复创建spring容器
 * @author Teacher陈
 */
@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    /**
     * @create by: Teacher陈
     * @description:重写setApplicationContext抽象方法
     * @create time: 2020/7/29 21:54
     * @param applicationContext
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtil.applicationContext = applicationContext;
    }
    /**
     * @create by: Teacher陈
     * @description: 获取bean
     * @create time: 2020/7/29 21:53
     * @return Object
     */
    public static Object getBean(String name) {
        return applicationContext.getBean(name);
    }

}
